1 /* 2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.management; 27 28 import com.sun.jmx.mbeanserver.Introspector; 29 import java.lang.annotation.Annotation; 30 import java.lang.reflect.Constructor; 31 import java.util.Arrays; 32 33 /** 34 * Describes a constructor exposed by an MBean. Instances of this 35 * class are immutable. Subclasses may be mutable but this is not 36 * recommended. 37 * 38 * @since 1.5 39 */ 40 public class MBeanConstructorInfo extends MBeanFeatureInfo implements Cloneable { 41 42 /* Serial version */ 43 static final long serialVersionUID = 4433990064191844427L; 44 45 static final MBeanConstructorInfo[] NO_CONSTRUCTORS = 46 new MBeanConstructorInfo[0]; 47 48 /** @see MBeanInfo#arrayGettersSafe */ 49 private final transient boolean arrayGettersSafe; 50 51 /** 52 * @serial The signature of the method, that is, the class names of the arguments. 53 */ 54 private final MBeanParameterInfo[] signature; 55 56 /** 57 * Constructs an <CODE>MBeanConstructorInfo</CODE> object. The 58 * {@link Descriptor} of the constructed object will include 59 * fields contributed by any annotations on the {@code 60 * Constructor} object that contain the {@link DescriptorKey} 61 * meta-annotation. 62 * 63 * @param description A human readable description of the operation. 64 * @param constructor The <CODE>java.lang.reflect.Constructor</CODE> 65 * object describing the MBean constructor. 66 */ 67 public MBeanConstructorInfo(String description, Constructor<?> constructor) { 68 this(constructor.getName(), description, 69 constructorSignature(constructor), 70 Introspector.descriptorForElement(constructor)); 71 } 72 73 /** 74 * Constructs an <CODE>MBeanConstructorInfo</CODE> object. 75 * 76 * @param name The name of the constructor. 77 * @param signature <CODE>MBeanParameterInfo</CODE> objects 78 * describing the parameters(arguments) of the constructor. This 79 * may be null with the same effect as a zero-length array. 80 * @param description A human readable description of the constructor. 81 */ 82 public MBeanConstructorInfo(String name, 83 String description, 84 MBeanParameterInfo[] signature) { 85 this(name, description, signature, null); 86 } 87 88 /** 89 * Constructs an <CODE>MBeanConstructorInfo</CODE> object. 90 * 91 * @param name The name of the constructor. 92 * @param signature <CODE>MBeanParameterInfo</CODE> objects 93 * describing the parameters(arguments) of the constructor. This 94 * may be null with the same effect as a zero-length array. 95 * @param description A human readable description of the constructor. 96 * @param descriptor The descriptor for the constructor. This may be null 97 * which is equivalent to an empty descriptor. 98 * 99 * @since 1.6 100 */ 101 public MBeanConstructorInfo(String name, 102 String description, 103 MBeanParameterInfo[] signature, 104 Descriptor descriptor) { 105 super(name, description, descriptor); 106 107 if (signature == null || signature.length == 0) 108 signature = MBeanParameterInfo.NO_PARAMS; 109 else 110 signature = signature.clone(); 111 this.signature = signature; 112 this.arrayGettersSafe = 113 MBeanInfo.arrayGettersSafe(this.getClass(), 114 MBeanConstructorInfo.class); 115 } 116 117 118 /** 119 * <p>Returns a shallow clone of this instance. The clone is 120 * obtained by simply calling <tt>super.clone()</tt>, thus calling 121 * the default native shallow cloning mechanism implemented by 122 * <tt>Object.clone()</tt>. No deeper cloning of any internal 123 * field is made.</p> 124 * 125 * <p>Since this class is immutable, cloning is chiefly of 126 * interest to subclasses.</p> 127 */ 128 public Object clone () { 129 try { 130 return super.clone() ; 131 } catch (CloneNotSupportedException e) { 132 // should not happen as this class is cloneable 133 return null; 134 } 135 } 136 137 /** 138 * <p>Returns the list of parameters for this constructor. Each 139 * parameter is described by an <CODE>MBeanParameterInfo</CODE> 140 * object.</p> 141 * 142 * <p>The returned array is a shallow copy of the internal array, 143 * which means that it is a copy of the internal array of 144 * references to the <CODE>MBeanParameterInfo</CODE> objects but 145 * that each referenced <CODE>MBeanParameterInfo</CODE> object is 146 * not copied.</p> 147 * 148 * @return An array of <CODE>MBeanParameterInfo</CODE> objects. 149 */ 150 public MBeanParameterInfo[] getSignature() { 151 if (signature.length == 0) 152 return signature; 153 else 154 return signature.clone(); 155 } 156 157 private MBeanParameterInfo[] fastGetSignature() { 158 if (arrayGettersSafe) 159 return signature; 160 else 161 return getSignature(); 162 } 163 164 public String toString() { 165 return 166 getClass().getName() + "[" + 167 "description=" + getDescription() + ", " + 168 "name=" + getName() + ", " + 169 "signature=" + Arrays.asList(fastGetSignature()) + ", " + 170 "descriptor=" + getDescriptor() + 171 "]"; 172 } 173 174 /** 175 * Compare this MBeanConstructorInfo to another. 176 * 177 * @param o the object to compare to. 178 * 179 * @return true if and only if <code>o</code> is an MBeanConstructorInfo such 180 * that its {@link #getName()}, {@link #getDescription()}, 181 * {@link #getSignature()}, and {@link #getDescriptor()} 182 * values are equal (not necessarily 183 * identical) to those of this MBeanConstructorInfo. Two 184 * signature arrays are equal if their elements are pairwise 185 * equal. 186 */ 187 public boolean equals(Object o) { 188 if (o == this) 189 return true; 190 if (!(o instanceof MBeanConstructorInfo)) 191 return false; 192 MBeanConstructorInfo p = (MBeanConstructorInfo) o; 193 return (p.getName().equals(getName()) && 194 p.getDescription().equals(getDescription()) && 195 Arrays.equals(p.fastGetSignature(), fastGetSignature()) && 196 p.getDescriptor().equals(getDescriptor())); 197 } 198 199 /* Unlike attributes and operations, it's quite likely we'll have 200 more than one constructor with the same name and even 201 description, so we include the parameter array in the hashcode. 202 We don't include the description, though, because it could be 203 quite long and yet the same between constructors. Likewise for 204 the descriptor. */ 205 public int hashCode() { 206 int hash = getName().hashCode(); 207 MBeanParameterInfo[] sig = fastGetSignature(); 208 for (int i = 0; i < sig.length; i++) 209 hash ^= sig[i].hashCode(); 210 return hash; 211 } 212 213 private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) { 214 final Class<?>[] classes = cn.getParameterTypes(); 215 final Annotation[][] annots = cn.getParameterAnnotations(); 216 return MBeanOperationInfo.parameters(classes, annots); 217 } 218 }